package com.ruoyi.api.hr;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.api.sms.ISMSService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.hr.personalFile.domain.*;
import com.ruoyi.hr.personalFile.service.IHrPersonnelFileService;
import com.ruoyi.result.ResultEnum;
import com.ruoyi.result.ResultView;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/api/personalFile")
public class ApiPersonnelFileController  extends BaseController{

  @Value("${sms-accessKeyId}")
  private String accessKeyId;
  @Value("${sms-secret}")
  private String secret;
  //    @Value("${sms-signName}")
  private String signName = "深圳市环资源科创";
  //验证码
  @Value("${sms-templateCode_acountInit}")
  private String smsTemplateCodeAcountInit;

  @Autowired
  private IHrPersonnelFileService hrPersonnelFileService;

  @Autowired
  private ISMSService ismsService;

  public void handleJsonData(HrPersonnelFile hrPersonnelFile){
    List<Family> familieList = hrPersonnelFile.getFamilieList();
    if(familieList!=null && familieList.size() > 0)
      hrPersonnelFile.setFamilieListJson(JSON.toJSONString(familieList));

    List<Education> educationList = hrPersonnelFile.getEducationList();
    if(educationList!=null && educationList.size() > 0)
      hrPersonnelFile.setEducationListJson(JSON.toJSONString(educationList));

    List<WorkExperience> workExperienceList = hrPersonnelFile.getWorkExperienceList();
    if(workExperienceList!=null && workExperienceList.size() > 0)
      hrPersonnelFile.setWorkExperienceListJson(JSON.toJSONString(workExperienceList));

    List<Emergency> emergencyList = hrPersonnelFile.getEmergencyList();
    if(emergencyList!=null && emergencyList.size() > 0)
      hrPersonnelFile.setEmergencyListJson(JSON.toJSONString(emergencyList));

    List<TrainExperience> trainExperienceList = hrPersonnelFile.getTrainExperienceList();
    if(trainExperienceList!=null && trainExperienceList.size() > 0)
      hrPersonnelFile.setTrainExperienceListJson(JSON.toJSONString(trainExperienceList));

    List<Certificate> certificateList = hrPersonnelFile.getCertificateList();
    if(certificateList!=null && certificateList.size() > 0)
      hrPersonnelFile.setCertificateListJson(JSON.toJSONString(certificateList));

    InductionMateria inductionMateria = hrPersonnelFile.getInductionMateria();
    if(inductionMateria !=null){
      hrPersonnelFile.setInductionMateriaJson(JSON.toJSONString(inductionMateria));
    }
  }
  @PostMapping("/commit")
  @ResponseBody
  public ResultView commit(@RequestBody HrPersonnelFile hrPersonnelFile) {
    handleJsonData(hrPersonnelFile);
    hrPersonnelFile.setSuspendState("0");//未批准
    hrPersonnelFile.setCreateTime(new Date());
    hrPersonnelFile.setUpdateTime(new Date());
    int rs=hrPersonnelFileService.insertHrPersonnelFile(hrPersonnelFile);

    if(rs > 0){
      //发送短信通知初始账户
      StringBuilder sb = new StringBuilder();
      sb.append("{\"tel\":\""+hrPersonnelFile.getPhone()+"\",\"pwd\":\"123456\"}");
      JSONObject o = ismsService.smsSend(sb.toString(), hrPersonnelFile.getPhone(), accessKeyId, secret, signName, smsTemplateCodeAcountInit);
      if (o != null && "OK".equalsIgnoreCase(o.getString("Code"))) {//成功,返回smscode
        logger.info("====短信发送成功==="+hrPersonnelFile.getPhone());
      }else {
        logger.error("====短信发送失败==="+hrPersonnelFile.getPhone());
      }
      return ResultView.ok();
    }else if(rs == 0){
      return ResultView.error(ResultEnum.CODE_1013);
    }else{
      return ResultView.error(ResultEnum.CODE_1014);
    }
  }


  @PostMapping("/list")
  @ResponseBody
  public ResultView list(@RequestBody  HrPersonnelFile hrPersonnelFile) {
    startPage();
    List<HrPersonnelFile> list = hrPersonnelFileService.selectHrPersonnelFileList(hrPersonnelFile);
    return ResultView.ok(getDataTable(list));
  }

  @PostMapping("/update/{id}")
  @ResponseBody
  public ResultView update(@PathVariable("id")Long id,@RequestBody HrPersonnelFile hrPersonnelFile) {
    handleJsonData(hrPersonnelFile);
    hrPersonnelFile.setUpdateTime(new Date());
    hrPersonnelFileService.updateHrPersonnelFileInfo(hrPersonnelFile);
    return ResultView.ok();
  }
  @PostMapping("/check/{id}")
  @ResponseBody
  public ResultView check(@PathVariable("id")Long id,@RequestParam String suspendState) {
    hrPersonnelFileService.check(id,suspendState);
    return ResultView.ok();
  }

  @PostMapping("/detail/{id}")
  @ResponseBody
  public ResultView detail(@PathVariable("id")Long id) {
    HrPersonnelFile hrPersonnelFile  = hrPersonnelFileService.selectHrPersonnelFileById(id);
    return ResultView.ok(hrPersonnelFile);
  }

}
